<?php

namespace p5\objects;

class CookieUtils
{


    public static function get($name, $defaultValue = "")
    {
        global $_COOKIE;
        if (array_key_exists($name, $_COOKIE)) {
            return unserialize($_COOKIE[$name]);
        }
        return $defaultValue;
    }

    public static function set($name, $value, $expire = "")
    {
        global $_COOKIE;
        if (empty($expire)) {
            $expire = time() + 30 * 24 * 60 * 60;
        }
        $real = serialize($value);
        setcookie($name, $real, $expire, "/");
    }

    public static function setAll($params, $expire = "")
    {
        global $_COOKIE;
        if (empty($params)) {
            return;
        }
        foreach ($params as $key => $value) {
            self::set($key, $value, $expire);
        }
    }

    public static function remove($name)
    {
        global $_COOKIE;
        setcookie($name, "", time() - 360000);
    }

    public static function removeAll()
    {
        global $_COOKIE;
        $protects = ["PHPSESSID"];
        $keys = array_keys($_COOKIE);
        foreach ($keys as $key) {
            if (in_array($key, $protects)) {
                continue;
            }
            self::remove($key);
        }
    }


}